import { createSlice, type PayloadAction } from '@reduxjs/toolkit';

// 定义状态类型
interface CounterState {
  value: number;
}

// 初始状态
const initialState: CounterState = {
  value: 0
};

// 创建 slice
const counterSlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    // 同步 action：增加计数
    increment: (state) => {
      state.value += 1; // 可以直接修改状态（内部使用 immer）
    },
    // 同步 action：减少计数
    decrement: (state) => {
      state.value -= 1;
    },
    // 同步 action：带参数的修改
    incrementByAmount: (state, action: PayloadAction<number>) => {
      state.value += action.payload;
    }
  }
});

// 导出 action creators
export const { increment, decrement, incrementByAmount } = counterSlice.actions;

// 导出 reducer
export default counterSlice.reducer;
